package C015;

import java.util.Stack;

/**
 * ClassName: MinStack
 * Package: C015
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/3 16:17
 * @Version 1.0
 * @Since 1.0
 */
public class MinStack2 {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());   //--> 返回 -3.
        minStack.pop();
        System.out.println(minStack.top());      //--> 返回 0.
        System.out.println(minStack.getMin());   //--> 返回 -2.
    }

    static class MinStack {
        Stack<Integer> dataStack = new Stack<>();
        Stack<Integer> minStack = new Stack<>();
        public MinStack() {

        }

        public void push(int val) {
            dataStack.push(val);
            if(minStack.isEmpty() || val < minStack.peek()) {
                minStack.push(val);
            }else {
                minStack.push(minStack.peek());
            }
        }

        public void pop() {
            dataStack.pop();
            minStack.pop();
        }

        public int top() {
            return dataStack.peek();
        }

        public int getMin() {
            return minStack.peek();
        }
    }

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
}
